package com.xaicode.utils;

import java.util.function.Consumer;
import java.util.function.Supplier;

/**
 * @author beborn xaicode@sina.com
 */
public class Stopwatch {

    public static final String DESC = "cost %d millis\n";
    public static final String DESC_START_STOP = "start on %d\nstop on %d\ncost %d millis\n";

    public static long elapsedTime(Supplier<?> supplier) {
        long start = System.currentTimeMillis();
        supplier.get();
        return System.currentTimeMillis() - start;
    }

    public static void printElapsedTime(Supplier<?> supplier) {
        System.out.printf(DESC, elapsedTime(supplier));
    }

    public static long elapsedTime(Consumer<?> consumer) {
        long start = System.currentTimeMillis();
        consumer.accept(null);
        return System.currentTimeMillis() - start;
    }

    public static void printElapsedTime(Consumer<?> consumer) {
        System.out.printf(DESC, elapsedTime(consumer));
    }

}
